<?php
/**
 * 会员管理
 *
 * @author chenfenghua <843958575@qq.com>
 * version 2.0
 */

namespace app\models\base;

use app\models\head\Head;
use app\models\head\Cmp;
use app\models\member\MemberAccount;
use app\models\member\MemberCode;
use app\models\member\MemberOther;
use app\models\member\MembersInfo;
use app\models\Utils;
use Yii;
use yii\db\Exception;
use yii\db\Command;
use app\models\base\SystemNotice;

class BMember extends Command
{
    /**
     * 验证验证码
     *
     * @param $login_account
     * @return
     */
    public function verifyCode($arr = [])
    {
        $model = Code::findOne($arr);
        if ($model) {
            if ($model['status'] == 1) {
                return false;//已验证
            } elseif (time() > $model['create_time'] + 30 * 3600) {
                return false;//过期
            } else {
                return true;
            }
        } else {
            return false;//无验证码
        }

    }



    /**
     * 注册会员
     * @param $headInfoArr
     * @param $verifyCodeArr
     * @return bool
     * @throws Exception
     */
    public function createUser($headInfoArr = [], $verifyCodeArr = [])
    {
        $command = Yii::$app->db;
        $transaction = $command->beginTransaction();
        $time = time();

        $head_info_arr = [
            'mobile' => $headInfoArr['mobile'],
            'password' => $headInfoArr['password'],
            'code' => $headInfoArr['code'],
            'create_time' => $time,
            'last_login_time' => $time,
            'create_ip' => \Yii::$app->request->userIP,
        ];
        //var_dump($head_info_arr);
        //$salt = $this->createSale();
        $head_login_arr = [
            'identity' => $verifyCodeArr['identity'],
            //'identity_id' => 1,
            'create_time' => $time,
            'last_modify' => $time,
        ];


        try {
            //用户信息
            $headModel = new Head();
            $headModel->attributes = $head_info_arr;
            $headModel->save();

            $head_id = $headModel->head_id;
            $head_mobile = $headModel->mobile;
            $head_login_arr['identity_id'] = $head_id;

            //登录日志保存

            $command->createCommand()->insert(
                LoginLog::tableName(),
             $head_login_arr
            )->execute();


            //修改验证码状态
            $command->createCommand()->update(
                Code::tableName(),
                ['status' => 1, 'last_modify' => $time,],
                'mobile = :mobile AND type = :type AND identity = :identity AND code = :code',
                $verifyCodeArr
            )->execute();

            //发送注册成功message
            $notice = new SystemNotice();
            $notice->type = 0;
            $notice->type_id = 0;
            $notice->identity = 2;
            $notice->identity_id = $head_id;
            $notice->create_time = $time;
            $notice->last_modify = $time;
            $notice->title = '账户注册';
            $notice->content = '尊敬的客户<span style="color:#53B9FC">['.$head_mobile.']</span><br>恭喜您,您的账户已经注册成功,账户ID:<span style="color:#53B9FC">'.$head_id.'</span>,如需推荐职位或正常使用更多服务则需事先进行身份认证<br><a style="color:#53B9FC" href="'.Yii::$app->params['web_url'].'/head/hunter-home/auth.html">[立即认证]</a>';
            $notice->save();

            $transaction->commit();
            return $head_id;
        } catch (Exception $e) {
            $transaction->rollBack();
            return false;
        }
    }


    /**
     * 修改手机号码
     * @param $head_id
     * @param array $headInfoArr
     * @param array $verifyCodeArr
     * @return bool
     */
    public function edit($head_id, $headInfoArr = [], $verifyCodeArr = [])
    {
        $command = Yii::$app->db;
        $transaction = $command->beginTransaction();

        try {
            //用户信息
            $headModel = Head::findOne($head_id);
            $headModel->attributes = $headInfoArr;
            $headModel->save();

            //修改验证码状态
            $command->createCommand()->update(
                Code::tableName(),
                ['status' => 1, 'last_modify' => time(),],
                'mobile = :mobile AND type = :type AND identity = :identity AND code = :code',
                $verifyCodeArr
            )->execute();


            $transaction->commit();
            return true;
        } catch (Exception $e) {
            $transaction->rollBack();
            return false;
        }
    }


}